/*******************************************************************************
 * Copyright (c) 2010 Marcelo Cataldo, Sangeeth Nambiar
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * SettingsViewPart.java
 * 
 * View which contains the controls for setting the node properties
 * 
 * Contributors:
 *    Marcelo Cataldo  - initial implementation 
 *    Sangeeth Nambiar - initial implementation
 *******************************************************************************/
package edu.cmu.isr.dinvenio.ui.views;

import java.util.Enumeration;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.part.ViewPart;

import edu.cmu.isr.dinvenio.Activator;

public class SettingsViewPart extends ViewPart {

	private Composite parent;
	private Group NodePropGroup;
	private Group colorTypeGroup;

	public SettingsViewPart() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		this.parent = parent;

		RowLayout l = new RowLayout(SWT.VERTICAL);
		l.fill = true;
		this.parent.setLayout(l);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}	
	
	public void addNodeProperties(Enumeration<String> nodeProperties) {			
		if (nodeProperties != null){
			
			this.clearNodeProperties();	
			
			NodePropGroup = new Group(parent, SWT.NONE);
			NodePropGroup.setText("Node Properties");		
			NodePropGroup.setLayout(new RowLayout(SWT.HORIZONTAL));
			
			colorTypeGroup = new Group(this.parent, SWT.NONE);
			colorTypeGroup.setText("Node Color Type");		
			colorTypeGroup.setLayout(new RowLayout(SWT.VERTICAL));
			
			final Button buttonRGCol = new Button(colorTypeGroup, SWT.RADIO);
			buttonRGCol.setText("Heat Map");
			buttonRGCol.setSelection(true);
			buttonRGCol.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {										
					if (buttonRGCol.getSelection() == true) {
						Activator.myController.setNodeColorStyleRG(true);
					}									
				}
			});
			
			final Button buttonHueCol = new Button(colorTypeGroup, SWT.RADIO);
			buttonHueCol.setText("Hue");
			buttonHueCol.setSelection(false);
			buttonHueCol.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {										
					if (buttonHueCol.getSelection() == true) {
						Activator.myController.setNodeColorStyleRG(false);
					}									
				}
			});
			
			Group nodeColorGroup = new Group(NodePropGroup, SWT.NONE);
			nodeColorGroup.setText("Node Color");		
			nodeColorGroup.setLayout(new RowLayout(SWT.VERTICAL));
			
			final Button buttonDefCol = new Button(nodeColorGroup, SWT.RADIO);
			buttonDefCol.setText("Default");
			buttonDefCol.setSelection(true);
			buttonDefCol.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {										
					if (buttonDefCol.getSelection() == true) {
						Activator.myController.setNodeColorProperty(null);
					}									
				}
			});
			
			Group nodeSizeGroup = new Group(NodePropGroup, SWT.NONE);
			nodeSizeGroup.setText("Node Size");		
			nodeSizeGroup.setLayout(new RowLayout(SWT.VERTICAL));
			
			final Button buttonDefSize = new Button(nodeSizeGroup, SWT.RADIO);
			buttonDefSize.setText("Default");
			buttonDefSize.setSelection(true);
			buttonDefSize.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {										
					if (buttonDefSize.getSelection() == true) {
						Activator.myController.setNodeSizeProperty(null);
					}									
				}
			});
						
			for (Enumeration<String> e = nodeProperties ; e.hasMoreElements() ;) {    
		        String property = e.nextElement();
		        
				final Button button1 = new Button(nodeColorGroup, SWT.RADIO);
				button1.setText(property);
				button1.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {										
						if (button1.getSelection() == true) {
							Activator.myController.setNodeColorProperty(button1.getText());
						}									
					}
				});
		        
		        final Button button2 = new Button(nodeSizeGroup, SWT.RADIO);
		        button2.setText(property);
		        button2.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {				
						if (button2.getSelection() == true) {
							Activator.myController.setNodeSizeProperty(button2.getText());
						}
					}
				});
		     }	
		}
		refreshView();
	}

	private void clearNodeProperties(){
		if (NodePropGroup != null){
			NodePropGroup.dispose();
		}
		if (colorTypeGroup != null){
			colorTypeGroup.dispose();
		}
	}
	
	public void refreshView(){
		parent.layout(true);
	}

}
